package com.fmy.jurisdiction.core.inits;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fmy.jurisdiction.core.commons.consts.SystemConsts;
import com.fmy.jurisdiction.modular.model.AdminDept;
import com.fmy.jurisdiction.modular.model.AdminRole;
import com.fmy.jurisdiction.modular.model.AdminUser;
import com.fmy.jurisdiction.modular.service.IAdminDeptService;
import com.fmy.jurisdiction.modular.service.IAdminRoleService;
import com.fmy.jurisdiction.modular.service.IAdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName InitCommon
 * @Description TODO 进行初始化的bean
 * @createTime 2019/9/16 12:45
 */
@Component
@Slf4j
public class InitCommon {

    @Autowired
    private IAdminUserService userService;
    @Autowired
    private IAdminRoleService roleService;
    @Autowired
    private IAdminDeptService deptService;

    @Async
    public void init() {
        initUser();
        initRole();
        initDept();
    }

    /**
     * 初始化用户
     */
    private void initUser() {
        if (userService.count(new QueryWrapper<AdminUser>().eq(AdminUser.ID, SystemConsts.USER_ADMIN_ID)) == 0) {
            try {
                userService.addAdmin();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                log.info("超级管理员初始化失败");
            }
        }
    }

    /**
     * 初始化角色
     */
    private void initRole() {
        if (roleService.count(new QueryWrapper<AdminRole>().eq(AdminRole.ID, SystemConsts.ROLE_ADMIN_ID)) == 0) {
            try {
                roleService.addAdminRole();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                log.info("超级管理员角色初始化失败");
            }
        }
    }

    /**
     * 初始化部门
     */
    private void initDept() {
        if (deptService.count(new QueryWrapper<AdminDept>().eq(AdminDept.ID, SystemConsts.DEPT_ADMIN_ID)) == 0) {
            try {
                deptService.addAdminDept();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                log.info("超级管理员角色初始化失败");
            }
        }
    }
}
